package com.kitty.http.pay;

import com.kitty.common.utils.SpringUtils;
import com.kitty.game.pay.model.PayNotifyInfo;
import com.kitty.game.pay.model.PayOrderResponse;
import com.kitty.game.pay.service.OrderService;
import org.nutz.json.Json;
import org.nutz.json.JsonFormat;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

@RestController
@RequestMapping("/pay")
public class PayController {
    @Autowired(required = false)
    HttpServletResponse response;

    @RequestMapping(method = RequestMethod.POST, path = "order")
    @ResponseBody
    public String order(String data) throws IOException {
        // TODO 只处理登录服的请求

        PayNotifyInfo payNotifyInfo = Json.fromJson(PayNotifyInfo.class, data);
        boolean handled = SpringUtils.getBean(OrderService.class).handleOrder(payNotifyInfo);
        PayOrderResponse payOrderResponse = new PayOrderResponse(payNotifyInfo.getTradeNo(), handled);
        return Json.toJson(payOrderResponse, JsonFormat.compact());
    }
}
